package com.rattsoftware.client.views;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.rattsoftware.client.AppMessages;
import com.rattsoftware.client.events.AppEvents;
import com.rattsoftware.client.model.CourseSummary;
import com.rattsoftware.client.model.CourseUnitSummary;
import com.rattsoftware.client.util.ProgressIndicator;
import com.rattsoftware.server.admin.model.RattsModelConstants;

public class NewEditCourseUnitView extends View {

	final AppMessages appMessages = GWT.create(AppMessages.class);

	// Course Unit Details
	private TextField<String> newCourseName;
	private TextField<String> newCourseUnitShortName;
	private TextField<String> newCourseUnitName;
	private HtmlEditor newCourseUnitDescription;
	private NumberField noOfCreditUnits;
	// Semester
	private RadioGroup semester;
	private Radio radioSemOne;
	private Radio radioSemTwo;

	// Course Unit Type
	private RadioGroup courseUnitType;
	private Radio radioElective;
	private Radio radioCore;
	
	// Course Unit Year
	private RadioGroup courseUnitYear;
	private Radio radioYearOne;
	private Radio radioYearTwo;
	private Radio radioYearThree;
	private Radio radioYearFour;
	private Radio radioYearFive;

	private CourseUnitSummary courseUnitSummary;
	private CourseSummary courseSummary;
	
	private Window window;

	public NewEditCourseUnitView(Controller controller) {
		super(controller);
	}

	@Override
	protected void handleEvent(AppEvent event) {
		GWT.log("NewEditCourseUnitView : handleEvent");
		if (event.getType() == AppEvents.NewCourseUnit) {
			courseSummary = event.getData();
			createUI("Add New Course Unit");
		} else if (event.getType() == AppEvents.EditCourseUnit) {
			courseUnitSummary = event.getData();
			createUI("Edit Course Unit Details for "
					+ courseUnitSummary.getCourseUnitName());
		}
	}

	private void createUI(String title) {
		window = new Window();
		window.setHeading(title);
		window.setModal(true);
		window.setPlain(true);
		window.setClosable(false);
		window.setMaximizable(false);
		window.setDraggable(true);
		window.setResizable(false);
		window.setSize(745, 495);
		window.setScrollMode(Scroll.AUTOY);
		window.setLayout(new FitLayout());

		ProgressIndicator.showProgressBar();
		window.add(getCourseUnitPanel());
		ProgressIndicator.hideProgressBar();
		window.show();
	}

	private FormPanel getCourseUnitPanel() {
		FormData formData = new FormData("100%");

		FormPanel panel = new FormPanel();
		panel.setFrame(true);
		panel.setHeaderVisible(false);
		panel.setSize(700, -1);
		panel.setLabelAlign(LabelAlign.TOP);

		LayoutContainer main = new LayoutContainer();
		main.setLayout(new ColumnLayout());

		LayoutContainer left = new LayoutContainer();
		left.setStyleAttribute("paddingRight", "10px");
		FormLayout layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		left.setLayout(layout);

		LayoutContainer leftEditor = new LayoutContainer();
		layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		leftEditor.setLayout(layout);

		LayoutContainer right = new LayoutContainer();
		right.setStyleAttribute("paddingLeft", "10px");
		layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		right.setLayout(layout);

		newCourseName = new TextField<String>();
		newCourseName.setFieldLabel("Course Name");

		if (courseSummary != null) {
			newCourseName.setValue(courseSummary.getCourseName());
		}

		newCourseName.setEnabled(false);
		left.add(newCourseName, formData);

		newCourseUnitShortName = new TextField<String>();
		newCourseUnitShortName.setFieldLabel("Course Unit Short Name");

		if (courseUnitSummary != null) {
			newCourseUnitShortName.setValue(courseUnitSummary
					.getCourseUnitCode());
		}

		right.add(newCourseUnitShortName, formData);

		newCourseUnitName = new TextField<String>();
		newCourseUnitName.setFieldLabel("Course Unit Name");

		if (courseUnitSummary != null) {
			newCourseUnitName.setValue(courseUnitSummary.getCourseUnitName());
		}
		left.add(newCourseUnitName, formData);

		noOfCreditUnits = new NumberField();
		noOfCreditUnits.setFieldLabel("No. Of Credit Units");

		if (courseUnitSummary != null) {
			noOfCreditUnits.setValue(Integer.parseInt(courseUnitSummary.getNoOfCreditUnits()));
		}

		right.add(noOfCreditUnits, formData);

		// Course Unit Year
		radioYearOne = new Radio();
		radioYearOne.setBoxLabel("Year One");

		radioYearTwo = new Radio();
		radioYearTwo.setBoxLabel("Year Two");

		radioYearThree = new Radio();
		radioYearThree.setBoxLabel("Year Three");

		radioYearFour = new Radio();
		radioYearFour.setBoxLabel("Year Four");

		radioYearFive = new Radio();
		radioYearFive.setBoxLabel("Year Five");

		if (courseUnitSummary != null) {
			if (courseUnitSummary.getYear().equalsIgnoreCase(RattsModelConstants.YEAR_ONE)) {
				radioYearOne.setValue(true);
			} else if (courseUnitSummary.getYear().equalsIgnoreCase(RattsModelConstants.YEAR_TWO)) {
				radioYearTwo.setValue(true);
			} else if (courseUnitSummary.getYear().equalsIgnoreCase(RattsModelConstants.YEAR_THREE)) {
				radioYearThree.setValue(true);
			} else if (courseUnitSummary.getYear().equalsIgnoreCase(RattsModelConstants.YEAR_FOUR)) {
				radioYearFour.setValue(true);
			} else if (courseUnitSummary.getYear().equalsIgnoreCase(RattsModelConstants.YEAR_FIVE)) {
				radioYearFive.setValue(true);
			}
		} else {
			radioYearOne.setValue(true);
		}

		courseUnitYear = new RadioGroup();
		courseUnitYear.setFieldLabel("Year");
		courseUnitYear.add(radioYearOne);
		courseUnitYear.add(radioYearTwo);
		courseUnitYear.add(radioYearThree);
		courseUnitYear.add(radioYearFour);
		courseUnitYear.add(radioYearFive);

		leftEditor.add(courseUnitYear, formData);
		//left.add(courseUnitYear, formData);

		// course types
		radioSemOne = new Radio();
		radioSemOne.setBoxLabel("One");

		radioSemTwo = new Radio();
		radioSemTwo.setBoxLabel("Two");

		semester = new RadioGroup();
		semester.setFieldLabel("Semester");
		semester.add(radioSemOne);
		semester.add(radioSemTwo);

		if (courseUnitSummary != null) {
			if (courseUnitSummary.getSemester().equalsIgnoreCase(
					RattsModelConstants.SEMESTER_ONE)) {
				radioSemOne.setValue(true);
			} else if (courseUnitSummary.getSemester().equalsIgnoreCase(
					RattsModelConstants.SEMESTER_TWO)) {
				radioSemTwo.setValue(true);
			}
		} else {
			radioSemOne.setValue(true);
		}

		left.add(semester, formData);

		// Course Duration
		radioCore = new Radio();
		radioCore.setBoxLabel("Core");

		radioElective = new Radio();
		radioElective.setBoxLabel("Elective");

		courseUnitType = new RadioGroup();
		courseUnitType.setFieldLabel("Course Unit Type");
		courseUnitType.add(radioCore);
		courseUnitType.add(radioElective);
		
		if (courseUnitSummary != null) {
			if (courseUnitSummary.getCourseUnitType().equalsIgnoreCase(
					RattsModelConstants.COURSE_UNIT_CORE)) {
				radioCore.setValue(true);
			} else if (courseUnitSummary.getCourseUnitType().equalsIgnoreCase(
					RattsModelConstants.COURSE_UNIT_ELECTIVE)) {
				radioElective.setValue(true);
			}
		} else {
			radioCore.setValue(true);
		}
		

		right.add(courseUnitType, formData);

		main.add(left, new ColumnData(.5));
		main.add(right, new ColumnData(.5));

		panel.add(main, new FormData("100%"));

		newCourseUnitDescription = new HtmlEditor();
		newCourseUnitDescription.setFieldLabel("Course Unit Description");
		newCourseUnitDescription.setHeight(200);
		
		if (courseUnitSummary != null) {
			newCourseUnitDescription.setValue(courseUnitSummary.getCourseUnitDescription());
		}
		
		leftEditor.add(newCourseUnitDescription, formData);

		panel.add(leftEditor, new FormData("100%"));

		Button buttonSave = new Button(appMessages.save());
		buttonSave.setIcon(IconHelper.createStyle("add"));
		buttonSave.addListener(Events.Select, new Listener<ButtonEvent>() {
			@Override
			public void handleEvent(ButtonEvent be) {
				// addNewCourse();
			}
		});
		
		Button buttonCancel = new Button(appMessages.cancel());
		buttonCancel.setIcon(IconHelper.createStyle("cancel"));
		buttonCancel.addListener(Events.Select, new Listener<ButtonEvent>() {
			@Override
			public void handleEvent(ButtonEvent be) {
				closeWindow();
			}
		});

		panel.addButton(buttonSave);
		panel.addButton(buttonCancel);

		FormButtonBinding binding = new FormButtonBinding(panel);
		binding.addButton(buttonSave);

		return panel;
	}
	
	@SuppressWarnings("deprecation")
	private void closeWindow() {

		ProgressIndicator.showProgressBar();
		window.close();
		ProgressIndicator.hideProgressBar();
	}
}
